package com.zz.common.app;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import com.zz.common.app.BaseApplication.IBaseApplicationattachment;
import com.zz.common.utils.ZLog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class BaseActivity extends Activity implements IBaseApplicationattachment {
	
	private static final String INDEX_LAST_TITLE_MIRROR = "last_title_mirror";
	
	private static WeakReference<BaseActivity> sTopActivityRef;
	private static int sActiveActivityCount;
	private static final ArrayList<WeakReference<BaseActivity>> sActivityStack = new ArrayList<WeakReference<BaseActivity>>();
	private String mLastTitleMirror;
	
	public static BaseActivity getTopActivity() {
		BaseActivity topActivity = null;
		if(null != sTopActivityRef) {
			topActivity = sTopActivityRef.get();
		}
		return topActivity;
	}
	
	public static ArrayList<WeakReference<BaseActivity>> getActivityStack() {
		return sActivityStack;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mLastTitleMirror = getIntent().getStringExtra(INDEX_LAST_TITLE_MIRROR);
		
		WeakReference<BaseActivity> curActivityRef = new WeakReference<BaseActivity>(this);
		synchronized (sActivityStack) {
			sActivityStack.add(curActivityRef);
		}
		
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_KEY, "onCreate: " + this + " TaskId: " + super.getTaskId());
	}
	
	@Override
	public void onStart() {
		super.onStart();
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_DEV, "onStart: " + this);
		
		int preActiveCount = sActiveActivityCount;
		sActiveActivityCount ++;
		if(preActiveCount == 0) {
			getBaseApplication().onEnterForeground();
		}
		
	}
	
	@Override
	public void onRestart() {
		super.onRestart();
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_DEV, "onRestart: " + this);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_DEV, "onResume: " + this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_DEV, "onPause: " + this);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_DEV, "onStop: " + this);
		sActiveActivityCount --;
		if(sActiveActivityCount == 0) {
			getBaseApplication().onEnterBackground();
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		synchronized (sActivityStack) {
			int size = sActivityStack.size();
			if(size > 0) {
				sActivityStack.remove(size - 1);
			}
		}
		
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_KEY, "onDestroy: " + this + " TaskId: " + super.getTaskId());
	}
	
	@Override	// BaseApplication.IBaseApplicationattachment
	public BaseApplication getBaseApplication() {
		return BaseApplication.getBaseApplication();
	}

	@Override
    public void startActivityForResult(Intent intent, int requestCode) {
		String titleMirror = onGetTitleMirror();
        intent.putExtra(INDEX_LAST_TITLE_MIRROR, titleMirror);
        super.startActivityForResult(intent, requestCode);
    }
    
	@Override
	protected void finalize() {
		ZLog.d(BaseConfig.TAG, ZLog.LOG_LEVEL_DEV, "finalize: " + this);
		try {
			super.finalize();
		} catch (Throwable e) {
			e = null;
		}
	}
	
	protected String onGetTitleMirror() {
		return "";
	}
	
	protected String getLastTitleMirror() {
		return mLastTitleMirror;
	}
}
